package com.xianyun.site.iblog.web.user;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.xianyun.site.iblog.api.user.SystemUserServiceI;
import com.xianyun.site.iblog.co.user.SystemUserCO;
import com.xianyun.site.iblog.dto.user.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 系统用户控制器
 * @author mac_zyj
 */
@RequestMapping("/api/consumer/user")
@RestController
public class SystemUserController {

    @Autowired
    private SystemUserServiceI systemUserService;


    @PostMapping("/info")
    public SingleResponse<SystemUserCO> getUserInfo(HttpServletRequest request){
        String token= request.getHeader("Token");
        SystemUserInfoQry systemUserInfoQry=new SystemUserInfoQry();
        systemUserInfoQry.setToken(token);
        systemUserInfoQry.setSessionId(request.getSession().getId());
        return systemUserService.getSystemUserInfoByToken(systemUserInfoQry);
    }

    @PostMapping("/password/change")
    public Response changePassword(@RequestBody UserPasswordChangeCmd userPasswordChangeCmd, HttpServletRequest request)
    {
        String token= request.getHeader("Token");
        userPasswordChangeCmd.setToken(token);
        userPasswordChangeCmd.setSessionId(request.getSession().getId());
        return systemUserService.changeUserPassword(userPasswordChangeCmd);
    }

    @CrossOrigin
    @PostMapping("/userName/change")
    public Response changeUserName(@RequestBody UserNameChangeCmd userNameChangeCmd, HttpServletRequest request){
        String token= request.getHeader("Token");
        userNameChangeCmd.setToken(token);
        userNameChangeCmd.setSessionId(request.getSession().getId());
        return systemUserService.updateUserName(userNameChangeCmd);
    }


    @PostMapping("/email/change")
    public Response changeNewEmail(@RequestBody EmailChangeCmd emailChangeCmd,HttpServletRequest request)
    {
        String token= request.getHeader("Token");
        emailChangeCmd.setToken(token);
        emailChangeCmd.setSessionId(request.getSession().getId());
        return systemUserService.changeNewEmail(emailChangeCmd);
    }

    @GetMapping("/email/{checkInfo}")
    public Response changeEmail(@PathVariable("checkInfo")String checkInfo)
    {
        return null;
    }



    @PostMapping("/info/update")
    public Response updateUserInfo(@RequestBody SystemUserUpdateCmd systemUserUpdateCmd, HttpServletRequest request){
        String token= request.getHeader("Token");
        systemUserUpdateCmd.setToken(token);
        systemUserUpdateCmd.setSessionId(request.getSession().getId());
        return systemUserService.updateUserInfo(systemUserUpdateCmd);
    }


    @PostMapping("/register")
    public Response registerUser(@RequestBody SystemUserAddCmd systemUserAddCmd, HttpServletRequest request){
        systemUserAddCmd.setRequest(request);
        return systemUserService.registerUser(systemUserAddCmd);
    }


    @GetMapping("/bind/social/{type}")
    @ApiOperation(value = "第三方绑定")
    public Response bindSocialUser(@ApiParam(name = "type", value = "登录类型", allowableValues = "gitee,oschina,alipay")
                               @PathVariable("type") String type, HttpServletResponse response, HttpServletRequest request) {
        return Response.buildSuccess();
    }
}
